<template>
    <Button type="primary" @click="isShow=!isShow"><span>新建购票人</span></Button>
    <Modal
        v-model="isShow"
        title="常用购票人"
        @on-cancel="resetForm">
        <Form ref="formItem" :model="formItem" :rules="ruleValidate" :label-width="80">
            <FormItem label="用户名" prop="name">
                <Input v-model="formItem.name" placeholder="请输入您的用户姓名"></Input>
            </FormItem>
            <FormItem label="身份证号" prop="idCard">
                <Input v-model="formItem.idCard" placeholder="请输入身份证号码"></Input>
            </FormItem>
            <FormItem label="用户名" prop="cardType">
                <Select v-model="formItem.cardType" placeholder="请输入卡类型">
                    <Option value="0">身份证</Option>
                    <Option value="1" disabled>军官证</Option>
                </Select>
            </FormItem>
        </Form>
        <template #footer>
            <Button @click="resetForm">取消</Button>
            <Button type="primary" @click="handleSubmit('formItem')">确定</Button>
        </template>
    </Modal>
</template>
<script>
import {validatebuyerexist,addticketbuyer} from "../../utils/api"
export default {
    data(){
        return {
            isShow:false,
            formItem:{
                name:'',
                idCard:'',
                cardType:0
            },
            ruleValidate:{
                name:[
                    {required:true,message:"姓名不能为空"}
                ],
                idCard:[
                    {required:true,message:"身份证号不能为空"},
                    {len:18,message:"身份证号是18位"}
                ],
                cardType:[
                    {required:true,message:"卡类型不能为空"}
                ]
            }
        }
    },
    emits:["then"],
    methods:{
        handleSubmit (name) {
            this.$refs[name].validate((valid) => {
                if (valid) {
                    this.add();
                    this.isShow=false;
                }
            })
        },
        resetForm(){
            this.isShow=false;
            this.formItem.name='';
            this.formItem.idCard='';
            this.formItem.cardType=0;
        },
        add(){
            validatebuyerexist(this.formItem.idCard).then(({data})=>{
                if(data.errorCode=='1009'){
                    this.$Message.warning('购票人已经存在，请重新添加哦');
                }else{
                    addticketbuyer(this.formItem).then(({data})=>{
                        if(data.data=='添加成功'){
                            this.$Message.success('添加成功！');
                            this.$emit("then");
                        }
                    })
                }
            })
        }
    }
}
</script>
<style>
    
</style>